package com.pengtu.gsj.controller.log;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pengtu.gsj.controller.base.BaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.app.Log;
import com.pengtu.gsj.service.system.LogService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.FileUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("/log/")
public class LogController extends BaseController{

	@Autowired
	private LogService logService;
	
	/**
	 * 
	 * loadEntity:
	 * 适用: 每个方法执行之前都会执行该方法
	 * @param id  log主键id
	 * @return 返回持久对象log或者新创建对象
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public Log loadEntity(String id){
		Log log ;
		if (StringUtils.isNotEmpty(id)) {
			log = logService.getLog(id);
		}else {
			log = new Log();
		}
		return log;
	}
	
	/**
	 * 
	 * show:
	 * 适用:显示log列表
	 * @param model 传递参数
	 * @return log列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	public String show(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		model.addAttribute("top", SpringMvcUtils.getParameter("top"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return  "system/log";
	}
	
	/**
	 * 
	 * getRoleList:
	 * 适用:显示所有日志列表
	 * @param model 传递参数
	 * @throws IOException 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "list", produces = "application/json; charset=utf-8")
	public void getCodes(Page<Log> page,
			HttpServletRequest request, Model model) throws IOException {
		List<Map<String,Object>> list = Lists.newArrayList();
			String path = ConfigUtils.getConfig("log.path");
			List<File> files = FileUtils.getDirFile(path);
			for(File f:files){
				Map<String,Object> map = Maps.newHashMap();
				map.put("fileName", f.getName());
				map.put("length",FileUtils.formetFileSize(f.length()));
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
				Date d = new Date(f.lastModified());  
				map.put("lastModified",format.format(d));
				list.add(map);
			}
			long totalCount = list.size();
			page.setTotalCount(totalCount);
			if(page.getPageNo()==page.getTotalPages()){
				list = list.subList((page.getPageNo()-1)*15, (int) totalCount);
			}
			else{
				list = list.subList((page.getPageNo()-1)*15, page.getPageNo()*15);
			}
			Collections.sort(list, new Comparator<Map<String, Object>>() {
				@Override
				public int compare(Map<String, Object> o1,
						Map<String, Object> o2) {
						 Date d1;
						 Date d2;
						try {
							d1 = DateUtils.convertStringToDate((String)o1.get("lastModified"));
							d2 = DateUtils.convertStringToDate((String)o2.get("lastModified"));
							   long map1value = d1.getTime();
				               long map2value = d2.getTime();
				               return (int)(map2value - map1value);
						} catch (ParseException e) {
							e.printStackTrace();
							return 0;
						}
			              
				}
	        });
			model.addAttribute("list", list);
	}
	/**
	 * getLogs:
	 * 适用:获取操作日志列表
	 * @param page
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="listInfo", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<Log> getLogs(Page<Log> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = logService.findLogs(page, queryFilter);
		return page;
	}

}
